﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities.Rules;
using System.Xml;
using System.Workflow.ComponentModel.Serialization;
using MWTWorkUnitMgmtTool.BusinessEntities;

namespace MWTWorkUnitMgmtTool.RulesEngine
{
    public class MappingRulesExecutor:IRulesExecutor
    {
        #region IRulesExecutor Members

        public void ExecuteRuleset<T>(T t) where T:class,new()
        {
            RuleSet ruleSet = null;
            string fileName = t.GetType().Name + ".rules";

            XmlTextReader rulesReader = new XmlTextReader(fileName);
            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
            ruleSet = (RuleSet)serializer.Deserialize(rulesReader);
            rulesReader.Close();

            RuleValidation validation = new RuleValidation(typeof(T), null);
            RuleExecution execution = new RuleExecution(validation, t);
            ruleSet.Execute(execution); 
            
        }

        public string Name()
        {
            return "RulesetExecutor"; 
        }

        #endregion
    }
}
